/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 16.11.2005
 * Time: 19:31
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

namespace MagicAttributeTest
{

    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
      private PlayerControl playerControl1;
      private ListBox lbCardDB;
      private UCHand ucHand1;
      private Button btnDoStuff;
      private UCInPlay ucInPlay1;
      private UCManaPool ucManaPool1;
      private UCStack ucStack1;
      private UCAbility ucAbility1;
      private Button btnInit;
    
        public MainForm()
        {
            Permanent card = new Permanent();
            ProtectionFromColor prot = new ProtectionFromColor("r");

            prot.AddToCard(card);

            EffectCall eCall = new EffectCall();
            eCall.method = MagicMethod.OnDamage;
            Damage damage = new Damage(5);
            eCall.parameter = damage;
            eCall.obj = card;

            Global.Resolve(eCall);


            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }


        #region Windows Forms Designer generated code
        /// <summary>
        /// This method is required for Windows Forms designer support.
        /// Do not change the method contents inside the source code editor. The Forms designer might
        /// not be able to load this method if it was changed manually.
        /// </summary>
        private void InitializeComponent()
        {
          this.btnInit = new System.Windows.Forms.Button();
          this.lbCardDB = new System.Windows.Forms.ListBox();
          this.btnDoStuff = new System.Windows.Forms.Button();
          this.ucManaPool1 = new MagicAttributeTest.UCManaPool();
          this.ucInPlay1 = new MagicAttributeTest.UCInPlay();
          this.ucHand1 = new MagicAttributeTest.UCHand();
          this.playerControl1 = new MagicAttributeTest.PlayerControl();
          this.ucStack1 = new MagicAttributeTest.UCStack();
          this.ucAbility1 = new MagicAttributeTest.UCAbility();
          this.SuspendLayout();
          // 
          // btnInit
          // 
          this.btnInit.Location = new System.Drawing.Point(82, 592);
          this.btnInit.Name = "btnInit";
          this.btnInit.Size = new System.Drawing.Size(163, 32);
          this.btnInit.TabIndex = 1;
          this.btnInit.Text = "Init";
          this.btnInit.UseVisualStyleBackColor = true;
          this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
          // 
          // lbCardDB
          // 
          this.lbCardDB.FormattingEnabled = true;
          this.lbCardDB.Location = new System.Drawing.Point(286, 22);
          this.lbCardDB.Name = "lbCardDB";
          this.lbCardDB.Size = new System.Drawing.Size(201, 147);
          this.lbCardDB.TabIndex = 2;
          // 
          // btnDoStuff
          // 
          this.btnDoStuff.Location = new System.Drawing.Point(82, 556);
          this.btnDoStuff.Name = "btnDoStuff";
          this.btnDoStuff.Size = new System.Drawing.Size(163, 30);
          this.btnDoStuff.TabIndex = 4;
          this.btnDoStuff.Text = "DoStuff";
          this.btnDoStuff.UseVisualStyleBackColor = true;
          this.btnDoStuff.Click += new System.EventHandler(this.btnDoStuff_Click);
          // 
          // ucManaPool1
          // 
          this.ucManaPool1.Location = new System.Drawing.Point(286, 282);
          this.ucManaPool1.Name = "ucManaPool1";
          this.ucManaPool1.Size = new System.Drawing.Size(154, 152);
          this.ucManaPool1.TabIndex = 6;
          // 
          // ucInPlay1
          // 
          this.ucInPlay1.Location = new System.Drawing.Point(446, 277);
          this.ucInPlay1.Name = "ucInPlay1";
          this.ucInPlay1.Size = new System.Drawing.Size(157, 157);
          this.ucInPlay1.TabIndex = 5;
          // 
          // ucHand1
          // 
          this.ucHand1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
          this.ucHand1.Location = new System.Drawing.Point(277, 462);
          this.ucHand1.Name = "ucHand1";
          this.ucHand1.Size = new System.Drawing.Size(761, 257);
          this.ucHand1.TabIndex = 3;
          // 
          // playerControl1
          // 
          this.playerControl1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
          this.playerControl1.Location = new System.Drawing.Point(24, 22);
          this.playerControl1.Name = "playerControl1";
          this.playerControl1.Size = new System.Drawing.Size(247, 441);
          this.playerControl1.TabIndex = 0;
          // 
          // ucStack1
          // 
          this.ucStack1.Location = new System.Drawing.Point(609, 277);
          this.ucStack1.Name = "ucStack1";
          this.ucStack1.Size = new System.Drawing.Size(159, 152);
          this.ucStack1.TabIndex = 7;
          // 
          // ucAbility1
          // 
          this.ucAbility1.Location = new System.Drawing.Point(774, 277);
          this.ucAbility1.Name = "ucAbility1";
          this.ucAbility1.Size = new System.Drawing.Size(154, 158);
          this.ucAbility1.TabIndex = 8;
          // 
          // MainForm
          // 
          this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
          this.ClientSize = new System.Drawing.Size(1060, 731);
          this.Controls.Add(this.ucAbility1);
          this.Controls.Add(this.ucStack1);
          this.Controls.Add(this.ucManaPool1);
          this.Controls.Add(this.ucInPlay1);
          this.Controls.Add(this.btnDoStuff);
          this.Controls.Add(this.ucHand1);
          this.Controls.Add(this.lbCardDB);
          this.Controls.Add(this.btnInit);
          this.Controls.Add(this.playerControl1);
          this.Name = "MainForm";
          this.Text = "MainForm";
          this.ResumeLayout(false);

        }
        #endregion

      private void btnInit_Click(object sender, EventArgs e)
      {
        Global.Cards = new CardDB();
        Global.Cards.InitDB();
        Global.Cards.ReadXmlCardFile("CardDB.xml");

        MagicWorld World = new MagicWorld();
        Global.World = World;
        StaticCard ragingGoblin = Global.Cards.GetCard("Raging Goblin");
        StaticCard lightningBolt = Global.Cards.GetCard("Lightning Bolt");
        StaticCard giantGrowth = Global.Cards.GetCard("Giant Growth");
        StaticCard ancientSilverback = Global.Cards.GetCard("Ancient Silverback");
        StaticCard mountain = Global.Cards.GetCard("Mountain");

        MagicPlayer player1 = new MagicPlayer();
        player1.Life = 20;
        player1.Hand.Add(new Permanent(ragingGoblin,  player1, MagicZone.Hand));
        player1.Hand.Add(new Permanent(lightningBolt, player1, MagicZone.Hand));
        player1.Hand.Add(new Permanent(giantGrowth, player1, MagicZone.Hand));
        player1.Hand.Add(new Permanent(ancientSilverback, player1, MagicZone.Hand));
        player1.Hand.Add(new Permanent(mountain, player1, MagicZone.Hand));
        foreach (Permanent perm in player1.Hand)
          Global.World.Permaments.Add(perm);

        World.Players.Add(player1);

        MagicPlayer player2 = new MagicPlayer();
        player2.Life = 20;
        player2.Hand.Add(new Permanent(ragingGoblin,  player2, MagicZone.Hand));
        player2.Hand.Add(new Permanent(lightningBolt, player2, MagicZone.Hand));
        player2.Hand.Add(new Permanent(giantGrowth,   player2, MagicZone.Hand));
        foreach (Permanent perm in player2.Hand)
          Global.World.Permaments.Add(perm);
        World.Players.Add(player2);

        Global.GuiPlayer = player1;
        playerControl1.Player = player1;
        playerControl1.UpdateGUI();

        Global.GuiState.PushStack(new NormalGUIAction());


        lbCardDB.Items.Clear();
        foreach (StaticCard card in Global.Cards.GetAllCards())
        {
          lbCardDB.Items.Add(card.CardName);
        }

        ucHand1.SetPlayer(player1);

        ucHand1.evActivateCard += new GUIPermanentEventHandler(ucHand1_evActivateCard);
        ucManaPool1.evManaPool += new GUIManaEventHandler(ucManaPool1_evMana);
        ucStack1.evResolve += new GUIEventHandler(ucStack1_evResolve);
        ucInPlay1.evClickCard += new GUIPermanentEventHandler(ucInPlay1_evClickCard);
        ucInPlay1.evRightClickCard += new GUIPermanentEventHandler(ucInPlay1_evRightClickCard);
        ucAbility1.evClickAbility += new GUIAbilityEventHandler(ucAbility1_evClickAbility);

        ucHand1.UpdateGUI();
        ucInPlay1.UpdateGUI();
        ucManaPool1.UpdateGUI();
      }

      void ucAbility1_evClickAbility(Permanent permanent, Ability ability)
      {
        Global.GuiState.ClickAbility(permanent, ability);
      }

      void ucInPlay1_evRightClickCard(Permanent permanent)
      {
        ucAbility1.SetPermanent(permanent);
        ucAbility1.UpdateGUI();
      }

      void ucInPlay1_evClickCard(Permanent permanent)
      {
        throw new Exception("The method or operation is not implemented.");
      }

      void ucStack1_evResolve()
      {
        Global.ResolveStack();
        UpdateGame();
      }

      void ucManaPool1_evMana(char Single)
      {
        Global.GuiState.ClickManaPool(Single);
        UpdateGame();
      }

      void ucHand1_evActivateCard(Permanent permanent)
      {
        Global.GuiState.ClickHandCard(permanent);
        UpdateGame();
      }

      private void btnDoStuff_Click(object sender, EventArgs e)
      {
        Global.DoGameActions();
        UpdateGame();
      }

      private void UpdateGame()
      {
        Global.DoGameActions();
        UpdateGUI();
      }

      private void UpdateGUI()
      {
        playerControl1.UpdateGUI();
        ucHand1.UpdateGUI();
        ucInPlay1.UpdateGUI();
        ucManaPool1.UpdateGUI();
        ucStack1.UpdateGUI();
      }
   }
}
